﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;
namespace WebUI.BL.Helper
{
    public class UploadFileBL
    {
        public void Insert(string action, string imgName, byte[] image, string createBy)
        {
            using(var dbcontext=new AdlerContextDataContext())
            {
                try
                {
                    int? outparam=0;
                    dbcontext.SP_ImageUpload(null,imgName,image,"Son",action, ref outparam);
                }

                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    dbcontext.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    dbcontext.SubmitChanges();
                }
            }
        }

        public IEnumerable<tbl_UploadedImagedetail> GetAllImg()
        {
            var dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_UploadedImagedetails.OrderByDescending(n=>n.CreatedDt);
        }

        public byte[] GetImgById(int id)
        {
            var dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_UploadedImagedetails.Where(x => x.ImageID == id)
                .Select(x => x.ImageContent).FirstOrDefault().ToArray();
        }
    }
}
